﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace IDReaderDemo
{
    public partial class Form1 : Form
    {
        private static int port = -1;

        public Form1()
        {
            InitializeComponent();
            OpenPort();
        }

        public void OpenPort()
        {
            try
            {
                int close = Identity.Close();
                if (close == 1)
                {
                    port = Identity.OpenAndGetPort();
                    statusLBPort.Text = port != -1 ? "串口号：" + port.ToString() : "";
                }
                if (port != -1) statusLB.Text = "串口已初始化";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void ClosePort()
        {
            try
            {
                int close = Identity.Close();
                port = close == 1 ? -1 : port;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            OpenPort();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                OpenPort();
                int read = Identity.ReadCard();
                ClosePort();
                statusLBAuth.Text = read == 1 ? "读取成功" : "读取失败，请检查卡片是否正确放置";
                Identity.Card card = read == 1 ? new Identity.Card() : null;
                SetInfo(card);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

        private void SetInfo(Identity.Card card)
        {
            if (card != null)
            {
                txtName.Text = card.Name;
                txtSex.Text = card.Sex;
                txtNation.Text = card.Nation;
                txtBirthDay.Text = card.BirthDay;
                txtAddress.Text = card.Address;
                txtIDCode.Text = card.IDCode;
                txtDepartment.Text = card.Department;
                txtStartDate.Text = card.StartDate;
                txtEndDate.Text = card.EndDate;
                MemoryStream ms = new MemoryStream(card.PhotoBMP);
                Image img = Image.FromStream(ms);
                picBox.Image = img;
            }
            else
            {
                txtName.Text = "";
                txtSex.Text = "";
                txtNation.Text = "";
                txtBirthDay.Text = "";
                txtAddress.Text = "";
                txtIDCode.Text = "";
                txtDepartment.Text = "";
                txtStartDate.Text = "";
                txtEndDate.Text = "";
                picBox.Image = null;
            }
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            ClosePort();
            if (port == -1) statusLB.Text = "串口已关闭";
        }

    }
}
